package oboe.utils;

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Matrix;

class TexturePool extends BitmapData
{
private var textureGridSize:Int;
private var textureTileSize:Int;

public function new(size:Int, textureCount:Int, transparent:Bool=true, fillColor:UInt=4.294967295E9)
{
	this.textureGridSize = 1;
	while( this.textureGridSize*this.textureGridSize < textureCount )
	{
	++this.textureGridSize;
	}
	
	this.textureTileSize = size/this.textureGridSize;

	super( size, size, transparent, fillColor);
}

private static inline var tmatrix:Matrix = new Matrix();
public function setTexture( texture:DisplayObject, id:Int ):Void
{
	var w:Float = (this.width-1)/this.textureGridSize;
	tmatrix.identity();
	var ty:Int = int( id/this.textureGridSize );
	var tx:Int = id%this.textureGridSize;
	tmatrix.ty = ty*this.textureTileSize;
	tmatrix.tx = tx*this.textureTileSize;
	tmatrix.d  = tmatrix.a  = this.textureTileSize/texture.width;
	this.textureTileSize/texture.height;
	this.draw( texture, tmatrix, null, null, null, true );
}
}